package	com.mes.web.controller.menu;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.menu.Menu;
import com.mes.entity.menu.RoleMenu;
import com.mes.service.menu.MenuService;
import com.mes.utils.DateJsonValueProcessor;
/**
 *
 *@ClassName: MenuController
 *@Description: 菜单表
 *@Author tdd
 *@Date 2019-02-12 15:31:00
 *@version 1.0
*/
@Controller
@RequestMapping("/Menu")
public class MenuController{
	@Resource MenuService MenuService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageMenu (Model model) {
		return "/td/Menu";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public Menu queryById(Long id) {
		Menu param = MenuService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<Menu> queryAllByMution(Menu param) {
		List<Menu> list = MenuService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(Menu param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<Menu> list = MenuService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = MenuService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("rows", jsonArray);
		result.put("total", total);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(result.toString());
		out.flush();
		out.close();
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(Menu param, HttpServletResponse response) throws Exception{
		Long id = MenuService.insert(param);
		response.getWriter().print(id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(Menu param,HttpServletResponse response) throws Exception{
		Integer count = MenuService.update(param);
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param param 实体条件
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = MenuService.delete(ids);
		response.getWriter().print(count);
	}
	
	/** 
	 * 条件查询多级菜单
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/childrenTreeByMution")
	@ResponseBody
	public List<Menu> childrenTreeByMution(RoleMenu param) {
		List<Menu> list = new ArrayList<>();
		List<Menu> menus = MenuService.queryAllByRid(param);
		for (int i = 0; i < menus.size(); i++) {
			Menu menu = menus.get(i);
			Menu menu2 = param.getMenu();
			menu2.setGrade("3");
			menu2.setPid(String.valueOf(menu.getMid()));
			param.setMenu(menu2);
			List<Menu> menuss = MenuService.queryAllByRid(param);
			if(menuss.size() > 0){
				menu.setChildren(menuss);
			}
			list.add(menu);
		}
		return list;
	}
}
